package com.ntv.persistent.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.ntv.persistent.PersistentException;
/**
 * It provides static methods for Date related functions
 * @author Kavi
 * @since 28/01/09
 */
public class DateHelper {
	/**
	 * @param timestamp
	 * @return date string ex . January 28, 2009 5:58 AM
	 */
	public static String getDateWithTime(Timestamp timestamp){
		Date date = new Date(timestamp.getTime());
		String parsedDate = DateFormat.getDateTimeInstance(
	            DateFormat.LONG, DateFormat.SHORT).format(date);
		return parsedDate;
	}
	
	public static String getParsedDateString(String strDate) throws PersistentException{
		SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yyyy");
		Date date = null;
		SimpleDateFormat sdfDestination = null;
		try {
			System.out.println(" strDate "+strDate);
			date = sdfSource.parse(strDate);
			sdfDestination = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			strDate = sdfDestination.format(date);
			
		} catch (ParseException e) {
			throw new PersistentException("Error occured while parsing the date string");
		}
		
		return strDate;
	}

	public static Date getParsedDate(String strDate) throws IllegalArgumentException{
		SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yyyy");
		Date date = null;
		try {
			System.out.println(" strDate "+strDate);
			date = sdfSource.parse(strDate);
		} catch (ParseException e) {
			throw new IllegalArgumentException("Error occured while parsing the date string:"+strDate);
		}
		
		return date;
	}
}
